#if defined _td_pocket_included
	#endinput
#endif

#include <hamsandwich>


#define _td_pocket_included
#define POCKET_NO_LIMIT -1
#define POCKET_LIMIT_GLOBAL -1

// This stores information on how many items of each type player has in his pocket
new playerPocketItems[MAX_PLAYERS][ITEMS];
new playerPocketLimits[MAX_PLAYERS][ITEMS];
new playerPocketLimitsGlobal[MAX_PLAYERS];

new cVarLooseOnDeath;
new gClearPocket[MAX_PLAYERS];

enum pocketLimitCheck{
	plcTrue,
	plcNoMore,
	plcFalse
}
enum pocketMneuItems{
	pmiAdd=1,
	pmiTake
}

stock register_def_pocket_cvars(){
	new cvar[64];
	formatex(cvar, charsmax(cvar), "%s_loose_on_death", prefix);
	cVarLooseOnDeath = register_cvar(cvar, CVAR_LOOSE_ON_DEATH_DEF);
}

stock plugin_init_pocket(){
	register_def_pocket_cvars();
	RegisterHam(Ham_DeathNotice, "player", "ham_death_pocket", 1);
	RegisterHam(Ham_Spawn, "player", "ham_spawn_pocket");
}

stock client_connect_pocket(id){
	for(new item=0;item<ITEMS;item++)
		td_set_pocket(id,item, 0);
	gClearPocket[id] = 0;
}

public ham_death_pocket(id){
	if(get_pcvar_num(cVarLooseOnDeath)){
		gClearPocket[id] = 1;
	}
}

public ham_spawn_pocket(id){
	if(gClearPocket[id]) {
		for(new item=0;item<ITEMS;item++)
			td_set_pocket(id,item, 0);
	}
	gClearPocket[id] = 0;
}
/*	This method sets how many items of specified type player can have in his pocket.
	If you do not want to limit this set limit as POCKET_NO_LIMIT
	If you want to set global limit (all items count limit) set item to POCKET_LIMIT_GLOBAL
*/
stock td_set_player_pocket_limit(id, limit, item){
	if(item == POCKET_LIMIT_GLOBAL)
		playerPocketLimitsGlobal[id] = limit;
	else
		playerPocketLimits[id][item] = limit;
}

/*	This method returns players pocket limit
	If item is POCKET_LIMIT_GLOBAL it will return global pocket limit
*/
stock td_get_player_pocket_limit(id, item){
	if(item == POCKET_LIMIT_GLOBAL)
		return playerPocketLimitsGlobal[id];
	else
		return playerPocketLimits[id][item];
}


/*
	This method adds item to players pocket
*/
stock td_add_to_pocket(id, item){
	playerPocketItems[id][item]++;
}

/*	This method sets count of item in players pocket
*/
stock td_set_pocket(id, item, count){
	playerPocketItems[id][item] = count
}

/*	This method takes item from poekct
*/
stock td_take_from_pocket(id, item){
	if(!td_can_get_item_from_pocket(id, item))
		return false;
	
	if(playerPocketItems[id][item]) {
		playerPocketItems[id][item]--;
		td_custom_item_get_from_pocket(id, item);
		return true;
	} else {
		client_print(id, print_chat, "%s %L", PREFIX_DISP, id, ML_STRINGS[mlNotInPocket], id, ITEM_NAMES[item]);
	}
	return false;
}
/*	This method checks if player can add items to pocket (global limit)
*/
stock pocketLimitCheck:td_can_add_to_pocket(id){
	new limit = playerPocketLimitsGlobal[id];
	switch(limit) {
		case POCKET_NO_LIMIT: return plcTrue;
		case 0: return plcFalse;
	}
	
	 if(limit > td_get_player_pocket_items_count(id))
		return plcTrue;
		
	return plcNoMore;
}
/*	This method returns coutn of all items in players pocket
*/
stock td_get_player_pocket_size(id){
	new count = 0;
	for(new item=0;item<ITEMS;item++)
		count += playerPocketItems[id][item];
	return count
}

/*	This method checks if item can be added to pocket (item limit)
*/
stock pocketLimitCheck:td_can_add_item_to_pocket(id, item){
	new limit = playerPocketLimits[id][item];
	switch(limit) {
		case POCKET_NO_LIMIT: return plcTrue;
		case 0: return plcFalse;
	}
	if( limit > playerPocketItems[id][item])
		return plcTrue;
		
	return plcNoMore;
}

/*	This displays menu with items player can take from pocket
*/
stock td_pocket_menu(id) {
	new text[64];
	new len = charsmax(text);
	formatex(text,  len, "/r%L", id, ML_STRINGS[mlPocketMenuTitle] );
	new menu = menu_create(text, "pocket_menu_handler");
	new info[1];
	
	new bool:canTake = td_get_player_pocket_size(id) > 0;
	
	if(!canTake){
		client_print(id, print_chat, "%s %L", PREFIX_DISP, id, ML_STRINGS[mlNoItemsInPocket]);
		return;
	}
	new count;
	for(new item=0;item<ITEMS;item++){
		count = playerPocketItems[id][item];
		if(count){
			text[0] = '^0';
			formatex(text, len, "%L (%d)", id, ITEM_NAMES[item],  count);
			info[0] = item;
			menu_additem(menu, text, info);
		}
	}
	menu_display(id, menu);
}

/*	Handler for taking items from pocket
*/
public pocket_menu_handler(id, menu, menuItem){
	if(menuItem == MENU_EXIT) {
		menu_destroy(menu);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, menuItem, acc, info, 1, _, _ , call);
	menu_destroy(menu);
	new item = info[0];
	
	td_take_from_pocket(id, item);
	
	td_pocket_menu(id);
}
